# -*- coding: utf-8 -*-
import configparser
import os

"""
 读取配置文件类
"""


class ReadConfig:
    """定义一个读取配置文件的类"""

    def __init__(self, filepath=None):
        try:
            if filepath:
                configpath = filepath
            else:
                if not os.path.exists("config"):
                    os.makedirs("config")
                root_dir = os.path.abspath('.')  # 获取当前路径
                configpath = os.path.join(root_dir, "config/config.conf")
            self.cf = configparser.ConfigParser()
            self.cf.read(configpath, encoding="utf-8")
        except:
            pass

    def get_host(self, section, param):
        """
           section:项目
           param:项目的参数
         """
        try:
            value = self.cf.get(section, param)
            return value
        except:
            pass



if __name__ == '__main__':
    """测试"""
    test = ReadConfig()
    t = test.get_host("ship", "server_host")
    print(t)
